/*
 * 树选择js
 */

var rec = new Object();
rec.count=0;
rec.trueIds="";
rec.falseIds="";
rec.trueId= new Array();
rec.falseId= new Array();
rec.checkArr = function(arr,value){
	var i = arr.length;
	while(i--){
		if (arr[i] === value){return true;}}
	return false;
};
//The seting childrens checked 设置子级选中状态
rec.setChildrens=function(_this){
	var childrens=_this.parent().siblings(".subTreeList").find(".ui_checkbox");
	var dd = "true";
	childrens.each(function(){
		var checkedcur = $(this).attr("checkedcur");
		if(checkedcur=="false"){
			dd="false";
			return;
		}else{
			dd="true"
		}
	});
	if(dd=="true"){
		childrens.attr("checkedcur","false");
		childrens.removeClass("trueFull").removeClass("truePart");
	}else{
		childrens.attr("checkedcur","true");
		childrens.addClass("trueFull").removeClass("truePart");
	}
}
//The seting checked 设置本身选中状态
rec.setOwn=function(_this){
	var checkedcur = _this.attr("checkedcur");
	if(checkedcur=="true"){
		_this.attr("checkedcur","false");
		_this.removeClass("trueFull").removeClass("truePart");
	} else if(checkedcur=="false"){
		_this.attr("checkedcur","true");
		_this.addClass("trueFull").removeClass("truePart");
	}
}

//The recursive 递归
rec.loopData=function(_obj,_this){
	var subSet = _obj.children(".subTreeList");
	var subSetList = subSet.children(".treeList");
	if(subSet.length>0){
		var ddArr = new Array(); 
		for(var i=0;i<subSetList.length;i++){
			rec.loopData(subSetList.eq(i));
			rec.count+=1;
			var checkbox = subSetList.eq(i).children(".ttName").children(".ui_checkbox");
			var checkedcur = checkbox.attr("checkedcur");
			if(checkedcur=="false")ddArr.push("false");else if(checkedcur=="true") ddArr.push("true"); else  ddArr.push("tf");
		}
		var rFalse = rec.checkArr(ddArr,"false");
		var rTrue = rec.checkArr(ddArr,"true");
		var rTf = rec.checkArr(ddArr,"tf");
		var checkbox = _obj.children(".ttName").children(".ui_checkbox");
		if(rFalse==true&&rTrue==false){
			checkbox.attr("checkedcur","false");
			checkbox.removeClass("trueFull").removeClass("truePart");
		} 
		if(rFalse==false&&rTrue==true){
			checkbox.attr("checkedcur","true");
			checkbox.addClass("trueFull").removeClass("truePart");
		} 
		if(rFalse==true&&rTrue==true){
			checkbox.attr("checkedcur","rTf");
			checkbox.addClass("truePart").removeClass("trueFull");
		} 
		if(rTf==true&&rTrue==true){
			checkbox.attr("checkedcur","rTf");
			checkbox.addClass("truePart").removeClass("trueFull");
		} 
		if(rTf==true&&rFalse==true){
			checkbox.attr("checkedcur","rTf");
			checkbox.addClass("truePart").removeClass("trueFull");
		}
	}
}

//The Screening checked 筛选出需要提交给数据库的Id
rec.setScreening=function(_checkObj){
	rec.trueIds="";
	rec.falseIds="";
	rec.trueId.length=0;
	rec.falseId.length=0;
	_checkObj.each(function(){
		var thisId = $(this).attr("userid");
		var checked = $(this).attr("checkeddefault");
		var checkedcur = $(this).attr("checkedcur");
		//选中
		if(checkedcur=="true"&&checked=="false"){
			rec.trueId.push(thisId);
		};
		//未选中
		if(checkedcur=="false"&&checked=="true"){
			rec.falseId.push(thisId);
		};
	});
	
	//将数组转化成字符串
	for(var i=0; i<rec.trueId.length;i++){
		rec.trueIds += rec.trueId[i]+";"
	}
	for(var i=0; i<rec.falseId.length;i++){
		rec.falseIds += rec.falseId[i]+";"
	}
}
rec.setScreening2=function(_checkObj){
	rec.trueIds="";
	rec.falseIds="";
	rec.trueId.length=0;
	rec.falseId.length=0;
	_checkObj.each(function(){
		var thisId = $(this).attr("userid");
		var checked = $(this).attr("checkeddefault");
		var checkedcur = $(this).attr("checkedcur");
		//选中
		if(checkedcur=="true"&&checked=="false"){
			rec.trueId.push(thisId);
		};
		if(checkedcur=="rTf"){
			rec.trueId.push(thisId);
		};
		//未选中
		if(checkedcur=="false"&&checked=="true"){
			rec.falseId.push(thisId);
		};
	});
	
	//将数组转化成字符串
	for(var i=0; i<rec.trueId.length;i++){
		rec.trueIds += rec.trueId[i]+";"
	}
	for(var i=0; i<rec.falseId.length;i++){
		rec.falseIds += rec.falseId[i]+";"
	}
}


